﻿using QW.Core.Cache;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QW.Core.Cache
{
    /// <summary>
    /// 缓存处理器
    /// </summary>
    public class CacheHandler
    {
        private static ICacheHandler _cache = null;//缓存接口
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="cache"></param>
        internal static void Register(ICacheHandler cache)
        {
            _cache = cache;
        }
        /// <summary>
        /// 获取缓存默认过期时间
        /// </summary>
        public static int GetDefaultTimeOut()
        {
            return _cache.DefaultTimeOut;
        }
        /// <summary>
        /// 获取缓存前缀
        /// </summary>
        public static string GetPrefix()
        {
            return _cache.Prefix;
        }
        /// <summary>
        /// 获取完整Key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetFullKey(string key)
        {
            return _cache.GetFullKey(key);
        }
        /// <summary>
        /// 缓存总数
        /// </summary>
        /// <returns></returns>
        public static int GetCount()
        {
            return _cache.GetCount();
        }
        /// <summary>
        /// 获取所有的key
        /// <para>会影响性能</para>
        /// </summary>
        /// <returns></returns>
        public static List<string> GetAllKeys()
        {
            return _cache.GetAllKeys();
        }
        /// <summary>
        /// 获得指定键的缓存值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="isFullKey">是否全称key</param>
        /// <returns></returns>
        public static T Get<T>(string key, bool isFullKey = false)
        {
            return _cache.Get<T>(key, isFullKey);
        }
        /// <summary>
        /// 从缓存中移除指定键的缓存值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="isFullKey">是否全称key</param>
        public static void Remove(string key, bool isFullKey = false)
        {
            _cache.Remove(key, isFullKey);
        }

        /// <summary>
        /// 将指定键的对象添加到缓存中
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <param name="data">缓存值</param>
        /// <param name="isFullKey">是否全称key</param>
        public static void Set(string key, object data, bool isFullKey = false)
        {
            _cache.Set(key, data, isFullKey);
        }

        /// <summary>
        /// 将指定键的对象添加到缓存中
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">缓存键</param>
        /// <param name="data">缓存值</param>
        /// <param name="isFullKey">是否全称key</param>
        public static void Set<T>(string key, T data, bool isFullKey = false)
        {
            _cache.Set<T>(key, data, isFullKey);
        }

        /// <summary>
        /// 将指定键的对象添加到缓存中，并指定过期时间
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <param name="data">缓存值</param>
        /// <param name="cacheTime">缓存过期时间(分钟)</param>
        /// <param name="isFullKey">是否全称key</param>
        public static void Set(string key, object data, int cacheTime, bool isFullKey = false)
        {
            _cache.Set(key, data, cacheTime, isFullKey);
        }

        /// <summary>
        /// 将指定键的对象添加到缓存中，并指定过期时间
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">缓存键</param>
        /// <param name="data">缓存值</param>
        /// <param name="cacheTime">缓存过期时间(分钟)</param>
        /// <param name="isFullKey">是否全称key</param>
        public static void Set<T>(string key, T data, int cacheTime, bool isFullKey = false)
        {
            _cache.Set<T>(key, data, cacheTime, isFullKey);
        }

        /// <summary>
        /// 将指定键的对象添加到缓存中，并指定过期时间
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <param name="data">缓存值</param>
        /// <param name="cacheTime">缓存过期时间</param>
        /// <param name="isFullKey">是否全称key</param>
        public static void Set(string key, object data, DateTime cacheTime, bool isFullKey = false)
        {
            _cache.Set(key, data, cacheTime, isFullKey);
        }

        /// <summary>
        /// 将指定键的对象添加到缓存中，并指定过期时间
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">缓存键</param>
        /// <param name="data">缓存值</param>
        /// <param name="cacheTime">缓存过期时间</param>
        /// <param name="isFullKey">是否全称key</param>
        public static void Set<T>(string key, T data, DateTime cacheTime, bool isFullKey = false)
        {
            _cache.Set(key, data, cacheTime, isFullKey);
        }
        /// <summary>
        /// 将指定键的对象添加到缓存中，并指定过期时间
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <param name="data">缓存值</param>
        /// <param name="cacheTime">缓存过期时间间隔</param>
        /// <param name="isFullKey">是否全称key</param>
        public static void Set(string key, object data, TimeSpan cacheTime, bool isFullKey = false)
        {
            _cache.Set(key, data, cacheTime, isFullKey);
        }

        /// <summary>
        /// 将指定键的对象添加到缓存中，并指定过期时间
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">缓存键</param>
        /// <param name="data">缓存值</param>
        /// <param name="cacheTime">缓存过期时间间隔</param>
        /// <param name="isFullKey">是否全称key</param>
        public static void Set<T>(string key, T data, TimeSpan cacheTime, bool isFullKey = false)
        {
            _cache.Set<T>(key, data, cacheTime, isFullKey);
        }
        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <param name="isFullKey">是否全称key</param>
        /// <returns></returns>
        public static bool Exists(string key, bool isFullKey = false)
        {
            return _cache.Exists(key, isFullKey);
        }
    }
}
